<?php

namespace loggedin\controller;

require_once("/loggedin/model/AddQuestion.php");
require_once("/loggedin/view/QuestionView.php");
require_once("/loggedin/model/Question.php");
require_once("/login/view/Userinfo.php");

class CreateQuestion {
	/**
	 * @var \loggedin\model\AddQuestion
	 */
	private $addQuestion;
	/**
	 * @var \loggedin\view\QuestionView
	 */
	private $questionView;
	/**
	 * @var \login\view\UserInfo
	 */
	private $userInfo;

	function __construct(\loggedin\view\QuestionView $questionView) {
		$this->addQuestion = new \loggedin\model\AddQuestion();
		$this->questionView = $questionView;
		$this->userInfo = new \login\view\UserInfo();
	}

	public function addQuestion() {
		$mysqli = new \mysqli("mysql08.citynetwork.se", "119958-mg18906",
							  "quizzer50", "119958-quizzer");
		$username = $this->userInfo->getUserLoggedinInfo();
		try {
			$question = $this->questionView->getNewQuestionInfo();
			$this->addQuestion->validateNewQuestion($question);
			$this->addQuestion->addToDatabase($username, $question, $mysqli);
			$this->questionView->setAddQuestionSuccessMessage();
		} catch (\Exception $e) {
			$this->questionView->setErrorMessage($e->getMessage());	
		}
		$mysqli->close();
	}
}